﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using DSServer.Banking;
using DSServer.Communication;

namespace DSServer.Server
{
    class Program
    {
        private static readonly ILog log = log4net.LogManager.GetLogger (typeof(Program));

        private static void configureLogging(string[] args){
            const string prefix = "--log4net.Config=";
            foreach (string arg in args)
            {
                if(arg.IndexOf(prefix)==0)
                {
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(arg.Substring(prefix.Length));
                    log4net.Config.XmlConfigurator.Configure(fileInfo);
                }
            }
        }

        static void Main(string[] args)
        {
            configureLogging(args);
            Ice.Communicator ic = null;
            try
            {
                ic = Ice.Util.initialize(ref args);

                Signer.Sign(ic);

                Ice.ObjectAdapter adapter = ic.createObjectAdapter("Adapter");
                
                BankI bankI = new BankI();
                CommunicatorI communicatorI = new CommunicatorI();

                adapter.addServantLocator( new BankingEvictor(),"Banking");
                adapter.addServantLocator( new CommunicationEvictor(), "Communication");

                adapter.add(bankI, ic.stringToIdentity("Banking/Bank"));
                adapter.add(communicatorI, ic.stringToIdentity("Communication/Communicator"));

                adapter.activate();
                ic.waitForShutdown();
            }
            catch (Exception e)
            {
                log.Error(e);
            }
            finally 
            {
                if ((ic != null))
                {

                    try
                    {
                        ic.destroy();
                    }
                    catch (Exception e)
                    {
                        log.Error(e);
                    }
                }
            }

        }
    }
}
